package com.gzp.topic;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 主题的生产者
 * 
 * @author Administrator
 *
 */
public class TestTopicProducer {

	private static final String brokerURL = "tcp://localhost:61616";
	private static final String topicName = "topic-hello";

	public static void main(String[] args) throws JMSException {
		ConnectionFactory factory = new ActiveMQConnectionFactory(brokerURL);
		Connection connection = factory.createConnection();
		connection.start();
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Topic topic = session.createTopic(topicName);
		MessageProducer messageProducer = session.createProducer(topic);
		for (int i = 0; i < 10; i++) {
			TextMessage textMessage = session.createTextMessage();
			textMessage.setText("topic-message" + i);
			messageProducer.send(textMessage);
		}
		messageProducer.close();
		session.close();
		connection.close();
		System.out.println("消息发送成功！");
	}
}
